package view;

import controller.ClientController;
import dto.Account;
import java.awt.Color;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

/**
 *
 * @author BLACK
 */

/**
 * ContactTree class to get the JTree off user contact list stored in DB.
 */ 
public class ContactTree {
    
    Vector<Account> contactList;
    JTree contactTree;
    ClientController controller;
    private static int notifies;
    
    ContactTree(Vector<Account> contactList)
    {
        this.contactList= contactList;
    }
    
    JTree getContactTree ()
    {
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("Contacts");
        
        DefaultMutableTreeNode onLine;
        DefaultMutableTreeNode offLine;
        DefaultMutableTreeNode child;
  
        onLine = new DefaultMutableTreeNode("Online Contacts:");
        top.add(onLine);
        
        offLine = new DefaultMutableTreeNode("Offline contacts:");
        top.add(offLine);
        
        if (contactList != null) {
            for (int i = 0; i < contactList.size(); i++) {
                String status = contactList.get(i).getStatus();
                if (status.equals("ONLINE") || status.equals("BUSY") || status.equals("AWAY")) {
                    child = new DefaultMutableTreeNode(contactList.get(i));
                    onLine.add(child);

                } else {
                    child = new DefaultMutableTreeNode(contactList.get(i));
                    offLine.add(child);
                }
            }
        }
        contactTree = new JTree(top);
        if (contactTree.getParent() != null) {
            System.out.println("tree not Not null");
        }
        /**
         * add CellRender to tree that will add suitable icons according to contact state.
         */
        contactTree.setCellRenderer(new CustomIconRenderer());


        return contactTree;
        
    }

    void setStatus(Account account) {
        Enumeration e = ((DefaultMutableTreeNode)contactTree.getModel().getRoot()).breadthFirstEnumeration();
        while (e.hasMoreElements()) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
            if (account.equals(node.getUserObject())) {
                String oldStatus = ((Account)node.getUserObject()).getStatus();
                ((Account)node.getUserObject()).setStatus(account.getStatus());
                if(((Account)node.getUserObject()).getStatus().equals("OFFLINE")) 
                    comeOnline_Offline(node, "Offline", new Icons().getOfflineIcon());
                else if((((Account)node.getUserObject()).getStatus().equals("ONLINE") ||
                        ((Account)node.getUserObject()).getStatus().equals("AWAY") ||
                        ((Account)node.getUserObject()).getStatus().equals("BUSY")) && oldStatus.equals("OFFLINE"))
                    comeOnline_Offline(node, "Online", new Icons().getOnlineIcon());
                break;
            }
        }
        contactTree.repaint();
        
//        for (int i = 0; i < contactList.size(); i++) {
//            if(account.equals(contactList.get(i)))
//                contactList.get(i).setStatus(account.getStatus());  
//        }
    }

    private void comeOnline_Offline(final DefaultMutableTreeNode node, final String status, final Icon icon) {
        node.removeFromParent();
        ((DefaultTreeModel)contactTree.getModel()).nodeStructureChanged(node);
        switch (status) {
            case "Online":
            case "Away":
            case "Busy":
                ((DefaultMutableTreeNode)((DefaultMutableTreeNode)contactTree.getModel().getRoot()).getChildAt(0)).add(node);
                break;
            case "Offline":
                ((DefaultMutableTreeNode)((DefaultMutableTreeNode)contactTree.getModel().getRoot()).getChildAt(1)).add(node);
                break;
        }
        ((DefaultTreeModel)contactTree.getModel()).nodeStructureChanged(node);
        JFrame f = null;
        final JDialog win = new JDialog(f, "Chat") {
            @Override
            protected void dialogInit() {
                notifies++;
                super.dialogInit();
                getContentPane().setBackground(Color.darkGray);
                JLabel lbl = new JLabel(node.getUserObject().toString() + " is " + status);
                lbl.setForeground(Color.WHITE);
                lbl.setIcon(icon);
                add(lbl);
            }
            
         };
        System.out.println(notifies);
        win.setBounds(java.awt.Toolkit.getDefaultToolkit().getScreenSize().width - 300, (notifies)*100, 300, 100);
        new Timer().schedule(new TimerTask() {

            @Override
            public void run() {
                notifies--;
                win.dispose();
            }
        }, 3000);
        
        win.setVisible(true);

        
    }

    void setClientController(ClientController clientController) {
        this.controller = clientController;
    }
    
}
